package com.wanger.swa.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanger.swa.convert.UserConvert;
import com.wanger.swa.dto.user.query.UserQuery;
import com.wanger.swa.dto.user.req.UserAddReq;
import com.wanger.swa.dto.user.req.UserUpdateReq;
import com.wanger.swa.dto.user.resp.UserResp;
import com.wanger.swa.domain.po.UserPO;
import com.wanger.swa.domain.mapper.UserMapper;
import com.wanger.swa.service.ise.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: wanger
 * @Date: 2024/9/15 12:52
 * @Description: 用户
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserService {

    private final UserConvert userConvert;

    @Override
    public UserResp addUser(UserAddReq addDTO) {
        UserPO user = userConvert.toSaveEntity(addDTO);
        this.save(user);
        return userConvert.toDetailsDTO(user);
    }

    @Override
    public UserResp updateUser(UserUpdateReq updateDTO) {
        UserPO user = userConvert.toUpdateEntity(updateDTO);
        this.updateById(user);
        user.setUpdateTime(null);
        return userConvert.toDetailsDTO(user);
    }

    @Override
    public UserResp getOneUser(String id) {
        UserPO user = this.getById(id);
        return userConvert.toDetailsDTO(user);
    }

    @Override
    public IPage<UserResp> pageUser(UserQuery query) {
        IPage<UserPO> page = page(new Page<>(query.getCurrent(), query.getSize()));
        return userConvert.toPageDTO(page);
    }

    @Override
    public List<UserResp> listUser(UserQuery query) {
        return this.pageUser(query).getRecords();
    }

    @Override
    public void deleteUser(String id) {
        this.removeById(id);
    }
}
